#!/usr/bin/python
# -*- coding:utf8 -*-
'''
初始化交易对
'''

import utils
import json

# 交易所主键
EXCHANGE_ID_ZB = 1
EXCHANGE_ID_HUOBI = 2
EXCHANGE_ID_OKEX = 3


class Currency:
    def __init__(self):
        self.u = utils.CoinTools()
        self.conn = self.u.mysqlconnect()
        self.cur = self.conn.cursor()

    def init_zb_currency(self):
        '''
        zb
        :return:
        '''
        url = """http://api.zb.com/data/v1/markets"""
        body = utils.CoinTools.doGet(url)
        sql = """insert into currency(exchange_id, currency_name) VALUES (%s, %s)"""
        if body:
            dic = json.loads(body, encoding='utf8')
            lis_currency = []
            for key in dic.keys():
                if key and key != '':
                    lis_currency.append((EXCHANGE_ID_ZB, key))
            if lis_currency:
                self.cur.executemany(sql, lis_currency)

    def init_huobi_currency(self):
        '''
        huobi
        :return:
        '''
        url = """https://api.huobi.pro/v1/common/symbols"""
        body = utils.CoinTools.doGet(url)
        if body:
            sql = """insert into currency(exchange_id, currency_name) VALUES (%s, %s)"""
            dic = json.loads(body)
            if dic['status'] == 'ok' and dic['data']:
                lis_currency = []
                for d in dic['data']:
                    base = d['base-currency']
                    quote = d['quote-currency']
                    currency_name = base + quote
                    if currency_name and currency_name != '':
                        lis_currency.append((EXCHANGE_ID_HUOBI, currency_name))
                if lis_currency:
                    self.cur.executemany(sql, lis_currency)

    def init_okex_currency(self):
        '''
        okex
        :return:
        '''
        pass

    def __del__(self):
        self.conn.commit()
        self.cur.close()
        self.conn.close()


if __name__ == '__main__':
    c = Currency()
    # c.init_zb_currency()
    c.init_huobi_currency()
    # c.init_okex_currency()
